package InnerInterface;

import java.util.Arrays;
import java.util.Comparator;

public class Student implements Comparable<Student>{
	//当我们这里不写泛型的时候默认的是object对象，
	//object对象相当于是我们任何类都继承它了，根据自己的需要，再去重写里面的方法。
	public String name;
	public int age;
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	@Override
	public int compareTo(Student stu) {
		return this.age-stu.age;
	}

	@Override
	public String toString() {
		return "Student{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}
	public static  void sort(Comparable[] array) {
		for(int i=0;i< array.length;i++) {
			//降序排列
			//参数为什么是这个
			//因为我们想让我们自己的数组对象去调用上面的方法，但是呢，这个方法是Comparable接口里面的，而我们这个类引入了这个接口，
			//要想调用上面的方法，我们必须要发生向上转型，才能调用上面的方法，对应的就是用这个数组来进行接收。
			for (int j = 0; j < array.length-1-i; j++) {
				if(array[j].compareTo(array[j+1])>0) {
					Comparable temp=array[j];
					array[j]=array[j+1];
					array[j+1]=temp;
				}
			}
		}
	}
	public static void func() {
		AgeCompartor ageCompartor=new AgeCompartor();
		Student student3=new Student("lisi",20);
		Student student4=new Student("lisi",244);
		System.out.println(ageCompartor.compare(student3, student4));
	}
	public static void func1() {
		NameCompartor nameCompartor=new NameCompartor();
		Student student5=new Student("张三",20);
		Student student6=new Student("李四",244);
		System.out.println(nameCompartor.compare(student6, student5));
	}
	public static void main(String[] args) {
		Student[] students=new Student[]{
				new Student("zhangsan",100),
				new Student("lisi",102),
				new Student("wangwu",103),
				new Student("zhaoliu",98),
				new Student("laoqi",10),
		};
		Student student1=new Student("zhangsan",12);
		Student student2=new Student("zhangsan",10);
		System.out.println(student1.compareTo(student2));
		System.out.println("==========");
		sort(students);
		System.out.println(Arrays.toString(students));
		System.out.println("=========================");
		func();
		System.out.println("==============");
		func1();
	}
}
class AgeCompartor implements Comparator<Student> {
	@Override
	public int compare(Student o1, Student o2) {
		return o1.age-o2.age;
	}
}
class NameCompartor implements Comparator<Student> {
	@Override
	public int compare(Student o1, Student o2) {
		return o1.name.compareTo(o2.name);
		//o1.name是一个引用的类型，还可以调用内部String的接口。
	}
}


